package jp.co.dailyReport.weeklyReport.sql;

import java.util.Calendar;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.weeklyReport.WeeklyReport;
import jp.co.dailyReport.Utilities;

/**
 * 週報情報を更新するクラスです。
 */
public class WeeklyReportUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(WeeklyReportUpdateExecuter.class);
	private WeeklyReport weeklyReport;

	public WeeklyReportUpdateExecuter(WeeklyReport weeklyReport) {
		this.weeklyReport = weeklyReport;
	}

	/**
	 * 週報IDを基に、週報情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update weeklyReport set projectId=?,staffId=?,workDateStart=?,workDateEnd=?," +
					"totalHoursWorked=?,problem=?,comment=?,upDay=? where weeklyReportId=?";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, weeklyReport.getProjectId());
			pstmt.setInt(2, weeklyReport.getStaffId());
			pstmt.setDate(3, new Date(weeklyReport.getWorkDateStart().getTime().getTime()));
			pstmt.setDate(4, new Date(weeklyReport.getWorkDateEnd().getTime().getTime()));
			pstmt.setDouble(5, weeklyReport.getTotalHoursWorked());
			pstmt.setString(6, Utilities.convUniqueCharacter(weeklyReport.getProblem()));
			pstmt.setString(7, Utilities.convUniqueCharacter(weeklyReport.getComment()));
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(8, new Date(cal.getTime().getTime()));
			pstmt.setInt(9, weeklyReport.getWeeklyReportId());
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}

}
